package com.xl.rentkeeper.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.xl.rentkeeper.R;
import com.xl.rentkeeper.view.AnimatedExpandableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/21.
 */
public class MsgFragment extends BaseFragment {
    private AnimatedExpandableListView listView;
    private ExampleAdapter adapter;
    //private SlidingLayer mSlidingLayer;
    @Override
    protected View initView() {
        //获取对应的布局
        View view = View.inflate(mActivity, R.layout.msg_controller, null);
        //用了显示小螺界面的listview列表
        listView = (AnimatedExpandableListView) view.findViewById(R.id.listView);
        return view;
    }
    @Override
    protected void initData(){
        List<GroupItem> items = new ArrayList<GroupItem>();

        // 模拟数据
        for (int i = 1; i < 100; i++) {
            GroupItem item = new GroupItem();

            item.title = "Group " + i;

            for (int j = 0; j < i; j++) {
                ChildItem child = new ChildItem();
                child.title = "Awesome item " + j;
                child.hint = "Too awesome";

                item.items.add(child);
            }

            items.add(item);

            //给listview设置对应的适配器
            adapter = new ExampleAdapter(mActivity);
            adapter.setData(items);

            listView.setAdapter(adapter);

//给group设置点击事件
            listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    // We call collapseGroupWithAnimation(int) and
                    // expandGroupWithAnimation(int) to animate group
                    // expansion/collapse.
                    if (listView.isGroupExpanded(groupPosition)) {
                        listView.collapseGroupWithAnimation(groupPosition);
                    } else {
                        listView.expandGroupWithAnimation(groupPosition);
                    }
                    return true;
                }

            });

            //给孩子设置点击事件
            listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                    final String TAG = "MsgController";
//                    getRootView().getContext().startActivity(intent);
//                    getRootView()..overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                    return false;


                }
            });
        }
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {

    }

    private static class GroupItem {
        String title;
        List<ChildItem> items = new ArrayList<ChildItem>();
    }

    private static class ChildItem {
        String title;
        String hint;
    }

    //孩子的Holder
    private static class ChildHolder {
        TextView title;
        TextView hint;
    }

    //group的Holder
    private static class GroupHolder {
        TextView title;
    }


    private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
        private LayoutInflater inflater;

        private List<GroupItem> items;

        public ExampleAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void setData(List<GroupItem> items) {
            this.items = items;
        }

        @Override
        public ChildItem getChild(int groupPosition, int childPosition) {
            return items.get(groupPosition).items.get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildHolder holder;
            ChildItem item = getChild(groupPosition, childPosition);
            if (convertView == null) {
                holder = new ChildHolder();
                convertView = inflater.inflate(R.layout.msg_list_item, parent, false);
                holder.title = (TextView) convertView.findViewById(R.id.msg_textTitle);
                holder.hint = (TextView) convertView.findViewById(R.id.msg_textHint);
                convertView.setTag(holder);
            } else {
                holder = (ChildHolder) convertView.getTag();
            }
            holder.title.setText(item.title);
            holder.hint.setText(item.hint);

            return convertView;
        }

        @Override
        public int getRealChildrenCount(int groupPosition) {
            return items.get(groupPosition).items.size();
        }

        @Override
        public GroupItem getGroup(int groupPosition) {
            return items.get(groupPosition);
        }

        //获取group的数量
        @Override
        public int getGroupCount() {
            return items.size();
        }

        //获取group的id
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder holder;
            //获取
            GroupItem item = getGroup(groupPosition);
            if (convertView == null) {
                holder = new GroupHolder();
                //加载group的item,
                convertView = inflater.inflate(R.layout.msg_group_item, parent, false);
                //获取group的title
                holder.title = (TextView) convertView.findViewById(R.id.msg_group_textTitle);
                convertView.setTag(holder);
            } else {
                holder = (GroupHolder) convertView.getTag();
            }
            //设置group的title
            holder.title.setText(item.title);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }
}
